package cn.chendd;

import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.support.ResourceRegion;
import org.springframework.http.HttpRange;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;

/**
 * 文件跳过指定大小读取示例
 * @author chendd
 * @date 2022/12/26 20:08
 */
public class Test {

    public static void main(String[] args) throws Exception {
        {
            List<HttpRange> ranges = HttpRange.parseRanges("bytes=37465308-");
            System.out.println(ranges);
            File file = new File("E:\\chendd\\Idea-Workspaces\\chendd-SpringCloud\\logs\\gateway\\gateway.root.log");
            System.out.println(file.length());
            List<ResourceRegion> regionList = HttpRange.toResourceRegions(ranges, new FileSystemResource(file));
            System.out.println(regionList);
            System.out.println(38553505 - 37465308);

            InputStream inputStream = new FileInputStream(file);
            inputStream.skip(38553505);
            byte[] bytes = new byte[1088197 - 3963 - 15 - 26 - 1 - 3 - 3];
            inputStream.read(bytes);
            System.out.println(new String(bytes));
        }
        {
            File file = new File("d:/utf8.txt");
            InputStream u8InputStream = new FileInputStream(file);
            byte[] u8bytes = new byte[5 + 3 + 3];
            u8InputStream.read(u8bytes);
            System.out.println(new String(u8bytes));
            System.out.println("utf8文件总字节：" + file.length());
        }

        {
            File file = new File("d:/gbk.txt");
            InputStream gbkInputStream = new FileInputStream(file);
            byte[] gbkbytes = new byte[5 + 2 + 2];
            gbkInputStream.read(gbkbytes);
            System.out.println(new String(gbkbytes , "GBK"));
            System.out.println("GBK文件总字节：" + file.length());
        }
    }

}
